<?php

use Drupal\store\Entity\Store;

function store_store_entity_form($form, &$form_state, Store $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'store') . '/store.forms.inc';
  $form_state[Store::class] = $e;

  $longitude = NULL;
  $latitude = NULL;
  $longitude = $e->getLongitude();
  $latitude = $e->getLatitude();

  $form['basic_info'] = [
    '#type' => 'fieldset',
    '#title' => t('基础信息'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['basic_info']['username'] = [
    '#type' => 'textfield',
    '#title' => t('负责人'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->username,
  ];
  $form['basic_info']['name'] = [
    '#type' => 'textfield',
    '#title' => t('门店名称'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['basic_info']['phone'] = [
    '#type' => 'textfield',
    '#title' => t('联系电话'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->phone,
  ];

  $form['basic_info']['address'] = [
    '#type' => 'textfield',
    '#title' => t('地址'),
    '#size' => 60,
    '#required' => TRUE,
    '#maxlength' => 128,
    '#default_value' => $e->address,
  ];

  $form['basic_info']['status'] = [
    '#type' => 'select',
    '#title' => t('状态.'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
  ];

  $form['basic_map'] = [
    '#type' => 'fieldset',
    '#title' => t('地图信息'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#prefix' => '<div id="map">',
    '#suffix' => '</div>',
  ];
  $form['basic_map']['search_address'] = [
    '#type' => 'textfield',
    '#title' => t('检索地址'),
    '#size' => 60,
    '#maxlength' => 128,
    '#description' => t('方便在地图中快速标记点.'),
  ];
  $form['basic_map']['search_address']['searchResultPanel'] = [
    '#type' => 'item',
    '#markup' => '<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto;display: none;"></div>',
  ];

  $form['basic_map']['container'] = [
    '#type' => 'container',
    '#prefix' => '<div id="container" class="map container">',
    '#suffix' => '</div>',
  ];

  $form['store']['basic_map']['coordinate'] = [
    '#type' => 'hidden',
    '#default_value' => $e->coordinate,
  ];
  $form['basic_map']['lng'] = [
    '#type' => 'hidden',
    '#default_value' => $longitude,
  ];
  $form['basic_map']['lat'] = [
    '#type' => 'hidden',
    '#default_value' => $latitude,
  ];

  $form['#attached']['js'][] = 'https://api.map.baidu.com/api?v=3.0&ak=' . variable_get_value('store_baidu_ak');
  $form['#attached']['js'][] = drupal_get_path('module', 'store') . '/store.js';

  $settings = [
    'lng' => $longitude,
    'lat' => $latitude,
  ];
  $form['#attached']['js'][] = [
    'data' => ['store' => $settings],
    'type' => 'setting',
  ];
  $form['#attached']['css'][] = drupal_get_path('module', 'store') . '/store.css';


  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  if ($e->id && user_access('store access delete')) {
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => t('删除'),
      '#submit' => ['store_entity_form_delete_submit'],
    ];
  }
  return $form;
}

function store_store_entity_form_submit($form, &$form_state) {
  $e = $form_state[Store::class];
  if ($e instanceof Store) {
    $e->username = $form_state['values']['username'];
    $e->name = $form_state['values']['name'];
    $e->phone = $form_state['values']['phone'];
    $e->address = $form_state['values']['address'];

    $longitude = $form_state['values']['lng'];
    $latitude = $form_state['values']['lat'];

    $ak = variable_get_value('store_baidu_ak_web');
    $sk = variable_get_value('store_baidu_sk_web');
    $uri = '/geoconv/v1/';
    $querystring_arrays = [
      'coords' => "{$longitude},{$latitude}",
      'ak' => $ak,
      'from' => 5,
      'to' => 3,
      'output' => 'json',
    ];
    $querystring = http_build_query($querystring_arrays);
    $sn = md5(urlencode($uri . '?' . $querystring . $sk));
    $url = "https://api.map.baidu.com{$uri}?{$querystring}&sn={$sn}";

    $e->longitude = $longitude;
    $e->latitude = $latitude;
    $result = drupal_http_request($url);
    if ($result->code == 200) {
      $data = $result->data;
      $data = drupal_json_decode($data);
      $bzb = $data['result'];
      $bzb = reset($bzb);
      $longitude = $bzb['x'];
      $latitude = $bzb['y'];
      $e->coordinate = 3;
      $e->longitude = $longitude;
      $e->latitude = $latitude;
    }
    $e->status = $form_state['values']['status'];
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function store_entity_form_delete_submit($form, &$form_state) {
  $e = $form_state[Store::class];
  if ($e instanceof Store) {
    $e->delete();
    drupal_set_message('删除成功');
  }
}
